package com.chanshuyi.dao.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.chanshuyi.model.User;
import com.chanshuyi.util.StringUtil;

import java.util.Map;

public class UserSqlProvider {

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbggenerated Mon Feb 01 16:13:55 CST 2016
     */
    public String insertSelective(User record) {
        BEGIN();
        INSERT_INTO("user");
        
        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=INTEGER}");
        }
        
        if (record.getUsername() != null) {
            VALUES("username", "#{username,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            VALUES("password", "#{password,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table user
     *
     * @mbggenerated Mon Feb 01 16:13:55 CST 2016
     */
    public String updateByPrimaryKeySelective(User record) {
        BEGIN();
        UPDATE("user");
        
        if (record.getUsername() != null) {
            SET("username = #{username,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            SET("password = #{password,jdbcType=VARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=INTEGER}");
        
        return SQL();
    }

    /************* 自定义查询 ***************/

    /**
     * 获取查询SQL
     * @param param
     * @return
     */
    public String getUserListByMapSql(Map<String, Object> param) {
        StringBuilder builder = new StringBuilder();
        Map<String, String> realParam = (Map<String, String>)param.get("param");

        //add select part
        builder.append(" select * from user where 1 = 1 ");

        //add condition part
        String conditionStr = "";
        if(!StringUtil.isNullOrEmpty(realParam)){
            conditionStr = getQueryCondition(realParam);
            if(!StringUtil.isNullOrEmpty(conditionStr)){
                builder.append(conditionStr);
            }
        }

        return new String(builder);
    }

    public String getQueryCondition(Map<String, String> param){
        StringBuilder builder = new StringBuilder();

        //if param is null or empty, return empty String
        if(param == null || param.size() < 1){
            return "";
        }

        for(String key : param.keySet()){
            String value = param.get(key);
            if(value != null && !value.isEmpty()){
                builder.append(" and " + key + " = '" + value + "'");
            }
        }

        return new String(builder);
    }
}